package com.ruicar.afs.cloud.channel.affiliation.controller;


import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruicar.afs.cloud.channel.affiliation.condition.AffiliationCondition;
import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsRelTemp;
import com.ruicar.afs.cloud.channel.affiliation.entity.ChannelAffiliatedUnitsTemp;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskAssign;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskCreated;
import com.ruicar.afs.cloud.channel.channelworkflow.entity.WorkTaskDetail;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskApproveRecordService;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskAssignService;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskCreatedService;
import com.ruicar.afs.cloud.channel.channelworkflow.service.WorkTaskDetailService;
import com.ruicar.afs.cloud.channel.common.condition.BaseInfoTypeDTO;
import com.ruicar.afs.cloud.channel.common.enums.ChannelAfsFlowEnum;
import com.ruicar.afs.cloud.channel.common.feign.ChannelUseCaseService;
import com.ruicar.afs.cloud.channel.common.mq.dto.ComAttachmentFileDto;
import com.ruicar.afs.cloud.channel.common.mq.sender.ChannelFileToApplySender;
import com.ruicar.afs.cloud.channel.common.until.ChannelConfig;
import com.ruicar.afs.cloud.channel.common.until.ChannelRoleEnum;
import com.ruicar.afs.cloud.channel.common.until.RoleTypeDic;
import com.ruicar.afs.cloud.channel.online.entity.ChannelBaseInfoTemp;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsRelTempService;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelAffiliatedUnitsTempService;
import com.ruicar.afs.cloud.channel.affiliation.service.ChannelBaseInfoTempService;
import com.ruicar.afs.cloud.channel.affiliation.utils.Common;
import com.ruicar.afs.cloud.channel.affiliation.vo.ChannelAffiliatedUnitsTempVo;
import com.ruicar.afs.cloud.channel.affiliation.vo.ChannelBaseInfoTempVo;
import com.ruicar.afs.cloud.channel.common.enmus.ChannelAffiiationEnum;
import com.ruicar.afs.cloud.channel.online.entity.ChannelAuthorizeRegionTemp;
import com.ruicar.afs.cloud.channel.online.service.ChannelAuthorizeRegionService;
import com.ruicar.afs.cloud.channel.online.untils.ChannelOnlineConstants;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.core.log.annotation.SysLog;
import com.ruicar.afs.cloud.common.core.security.util.SecurityUtils;
import com.ruicar.afs.cloud.common.core.uid.UidGenerator;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ChannelBelongEnum;
import com.ruicar.afs.cloud.common.mq.rabbit.message.AfsTransEntity;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.image.entity.ComAttachmentFile;
import com.ruicar.afs.cloud.image.service.ComAttachmentFileService;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.WorkFlowResponse;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.request.QueryFLowCmdRequest;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.request.StartFlowRequest;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.request.TaskSubmitRequest;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.response.FlowCmdBean;
import com.ruicar.afs.cloud.workflow.gwt.service.WorkflowService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.*;

import static com.ruicar.afs.cloud.channel.online.untils.ChannelOnlineConstants.BUSINESS_TYPE_NEW_CAR;

/**
 * @author panhj
 * @date 2020/5/19
 * ChannelAffiliatedUnitsTempController class function description.
 */
@Slf4j
@RestController
@AllArgsConstructor
@Api("渠道挂靠接口")
@RequestMapping("/channelAffiliated")
public class ChannelAffiliatedUnitsTempController {
    private ChannelAffiliatedUnitsTempService channelAffiliatedUnitsTempService;
    private ChannelAffiliatedUnitsRelTempService channelAffiliatedUnitsRelTempService;
    private ChannelBaseInfoTempService channelBaseInfoTempService;
    private ChannelAuthorizeRegionService channelAuthorizeRegionService;
    private ChannelUseCaseService channelUseCaseService;
    private ChannelConfig channelConfig;
    private final WorkflowService workflowService;
    private final UidGenerator uidGenerator;
    private final WorkTaskDetailService workTaskDetailService;
    private final WorkTaskAssignService workTaskAssignService;
    private final WorkTaskCreatedService workTaskCreatedService;
    private final WorkTaskApproveRecordService workTaskApproveRecordService;
    private final ComAttachmentFileService comAttachmentFileService;
    private final ChannelFileToApplySender channelFileToApplySenderl;

    /**
     * 此方法查询所有的合作商名称
     *
     * @return
     */
    @RequestMapping(value = "/getBaseInfo", method = RequestMethod.POST)
    @ApiOperation(value = "查询")
    public IResponse<List<ChannelBaseInfoTemp>> getAll() {
        List<ChannelBaseInfoTemp> list = channelBaseInfoTempService.list();
        return new IResponse<List<ChannelBaseInfoTemp>>().setData(list);
    }

    @PostMapping(value = "/getChannelInfo")
    @ApiOperation(value = "获取挂靠信息")
    public IResponse<IPage<List<ChannelAffiliatedUnitsTempVo>>> getChannelInfo(@RequestBody AffiliationCondition channel) {
        String date = "";
        if (channel.getStartDate() == null || ("").equals(channel.getStartDate())) {
            channel.setStartDate(date);
        }
        if (channel.getEndDate() == null || ("").equals(channel.getEndDate())) {
            channel.setEndDate("");
        }
        List statusList = new ArrayList();
        if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
            statusList.add(ChannelAffiiationEnum.REVIEW.getCode());
            statusList.add(ChannelAffiiationEnum.DEFAULT.getCode());
            statusList.add(ChannelAffiiationEnum.BACK.getCode());
            statusList.add(ChannelAffiiationEnum.START.getCode());
            statusList.add(ChannelAffiiationEnum.STOP.getCode());
            statusList.add(ChannelAffiiationEnum.REFUSE.getCode());
        } else {
            statusList.add(ChannelAffiiationEnum.REVIEW.getCode());
            statusList.add(ChannelAffiiationEnum.DEFAULT.getCode());
            statusList.add(ChannelAffiiationEnum.BACK.getCode());
            statusList.add(ChannelAffiiationEnum.START.getCode());
            statusList.add(ChannelAffiiationEnum.STOP.getCode());
        }
        channel.setQueryStatus(statusList);
        channel.setStart(ChannelAffiiationEnum.START.getCode());
        channel.setStop(ChannelAffiiationEnum.STOP.getCode());
        channel.setRefuse(ChannelAffiiationEnum.REFUSE.getCode());
        channel.setReview(ChannelAffiiationEnum.REVIEW.getCode());
        channel.setDefaultStatus(ChannelAffiiationEnum.DEFAULT.getCode());
        channel.setBack(ChannelAffiiationEnum.BACK.getCode());
        Page page = new Page(channel.getPageNumber(), channel.getPageSize());
        //  businessType：01是新车  02 二手车  03是非直营新车二手车都有
        // ownBusinessType ：01是新车  02 二手车  03  是直营新车二手车都有

        // 第一步我要知道登录者的角色属性  是属于直营还是非直营
        List<String> role =SecurityUtils.getRoles();
        BaseInfoTypeDTO dto =new BaseInfoTypeDTO();
        List belongList=new ArrayList();
        if(role.contains(ChannelRoleEnum.ROLE_ADMIN.name())){
            // 说明进来的是管理员
            dto.setBusinessType(RoleTypeDic.ALL_CAR);
            dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);
            belongList.add(AfsEnumUtil.key(ChannelBelongEnum.DIRECT));
            belongList.add(AfsEnumUtil.key(ChannelBelongEnum.SP));
            belongList.add(AfsEnumUtil.key(ChannelBelongEnum.ALL_TO_ALL));
            dto.setChannelBelong(belongList);
        }else {
            if(role.contains(ChannelRoleEnum.ROLE_OTHER_MANAGE_THIRD_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_SEE_THIRD_AFFILIATED.name())
                || role.contains(ChannelRoleEnum.ROLE_OTHER_MANAGE_NET_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_SEE_NET_AFFILIATED.name())){
                // 说明我有非直营权限
                dto.setBusinessType(RoleTypeDic.ALL_CAR);
                belongList.add(AfsEnumUtil.key(ChannelBelongEnum.SP));
                belongList.add(AfsEnumUtil.key(ChannelBelongEnum.ALL_TO_ALL));
                dto.setChannelBelong(belongList);
            }
            if(role.contains(ChannelRoleEnum.ROLE_OWN_MANAGE_THIRD_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OWN_SEE_THIRD_AFFILIATED.name())
            ||role.contains(ChannelRoleEnum.ROLE_OWN_MANAGE_NET_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OWN_SEE_NET_AFFILIATED.name())){
                // 说明我有直营权限
                dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);
                belongList.add(AfsEnumUtil.key(ChannelBelongEnum.DIRECT));
                dto.setChannelBelong(belongList);
            }
        }
        IPage<List<ChannelAffiliatedUnitsTempVo>> list = null;
        if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
            list = channelAffiliatedUnitsTempService.getChannelAffiliatedUnitsTempApply(page, channel);
        } else {
            if((dto.getBusinessType()==null||dto.getBusinessType()=="")&&(dto.getOwnBusinessType()==null||dto.getOwnBusinessType()=="")){
                return IResponse.fail("暂无权限查询！");
            }
            Map<String,String> headers = new HashMap<String,String>();
            headers.put("clientId",channelConfig.getCaseClientId());
            headers.put("clientSecret",channelConfig.getCaseClientSecret());
            IResponse result=channelUseCaseService.getBaseInfoByRole(dto,headers);
            if("0000".equals(result.getCode())){
                List<Long> ids =new ArrayList<>();
                List<ChannelBaseInfoTemp> base= JSON.parseArray(JSON.toJSONString(result.getData()), ChannelBaseInfoTemp.class);
                if(base.size()>0) {
                    for (int i = 0; i < base.size(); i++) {
                        ids.add(base.get(i).getId());
                    }
                    list = channelAffiliatedUnitsTempService.getChannelAffiliatedUnitsTempInfo(page, channel,ids);
                }
                }
        }
        return IResponse.success(list);
    }

    @PostMapping(value = "/deleteInfo")
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation("删除挂靠公司")
    public IResponse<Boolean> deleteInfo(@RequestBody AffiliationCondition channel) {
        channelAffiliatedUnitsTempService.removeById(channel.getId());
        return new IResponse<Boolean>().setMsg("删除成功");
    }

    @PostMapping(value = "/addAffiliationInfo")
    @ApiOperation(value = "添加第三方挂靠公司信息")
    @SysLog("添加第三方挂靠公司信息")
    public IResponse addAffiliationInfo(@Valid @RequestBody AffiliationCondition channel) throws JsonProcessingException {
        String saveOrCommit = channel.getSaveOrCommit();
        channel.setApplyTime(new Date());
        ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = new ChannelAffiliatedUnitsRelTemp();
        //根据社会代码以及挂靠类型查询是否已存在挂靠公司
        List<ChannelAffiliatedUnitsTemp> oldInfo = channelAffiliatedUnitsTempService.list(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                .eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, channel.getSocUniCrtCode())
                .eq(ChannelAffiliatedUnitsTemp::getBusiType, channel.getBusiType())
                .notIn(ChannelAffiliatedUnitsTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
        if (oldInfo.size() > 0) {
            for (int i = 0; i < oldInfo.size(); i++) {
                if ((ChannelAffiiationEnum.START.getCode()).equals(oldInfo.get(i).getStatus())) {
                    List idList = new ArrayList();
                    for (int k = 0; k < oldInfo.size(); k++) {
                        List<ChannelAffiliatedUnitsRelTemp> list = channelAffiliatedUnitsRelTempService.list(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, oldInfo.get(k).getId())
                                .notIn(ChannelAffiliatedUnitsRelTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
                        if (list != null) {
                            for (int j = 0; j < list.size(); j++) {
                                idList.add(list.get(j).getChannelId());
                            }
                        }
                    }
                    int channelCount = 0;
                    if (idList != null) {
                        for (int m = 0; m < idList.size(); m++) {
                            if ((channel.getChannelId()).equals(idList.get(m))) {
                                channelCount += 1;
                            }
                        }
                    } else {
                        channelCount = 0;
                    }
                    if (channelCount > 0) {
                        return IResponse.success(Boolean.FALSE).setMsg("该挂靠单位存在此合作商备案记录");
                    } else {
                        //判断总剩余额度
                        if (oldInfo.get(i).getTotalSurplusNum() <= 0) {
                            return IResponse.fail("挂靠单位额度已用尽");
                        } else {
                            channel.setCreateBy(SecurityUtils.getUsername());
                            channel.setCreateTime(new Date());
                            if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
                                channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                                channelAffiliatedUnitsTempService.save(channel);
                                channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                                channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
                                channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                                channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                                channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                                channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                                return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel);
                            } else {
                                ChannelAffiliatedUnitsTemp channelAffiliatedUnitsTemp = channelAffiliatedUnitsTempService.getOne(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                                        .eq(ChannelAffiliatedUnitsTemp::getBusiType, channel.getBusiType())
                                        .eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, channel.getSocUniCrtCode())
                                        .eq(ChannelAffiliatedUnitsTemp::getStatus, ChannelAffiiationEnum.START.getCode()));
                                if (saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode())) {
                                    channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                    channel.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                                    channel.setTotalSurplusNum(channel.getTotalApplicationNum());
                                    channel.setTotalApplicationNum(channel.getTotalApplicationNum());
                                    channel.setStatus(ChannelAffiiationEnum.DEFAULT.getCode());
                                    channelAffiliatedUnitsTempService.save(channel);
                                    channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
                                    channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.DEFAULT.getCode());
                                    channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                                    channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                                    channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                                    channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                                    return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel);
                                } else if (saveOrCommit.equals(ChannelAffiiationEnum.COMMIT.getCode())) {
                                    int totalAll = channelAffiliatedUnitsTemp.getTotalApplicationNum();
                                    int totalMonth = channelAffiliatedUnitsTemp.getMonthlyApplicationNum();
                                    int totalSurplusMonth = channelAffiliatedUnitsTemp.getMonthlySurplusNum();
                                    int totalSurplusAll = channelAffiliatedUnitsTemp.getTotalSurplusNum();
                                    int total = channel.getTotalApplicationNum() - (totalAll - totalSurplusAll);
                                    int month = channel.getMonthlyApplicationNum() - (totalMonth - totalSurplusMonth);
                                    Long id=channelAffiliatedUnitsTemp.getId();
                                    BeanUtils.copyProperties(channel, channelAffiliatedUnitsTemp);
                                    channelAffiliatedUnitsTemp.setId(id);
//                                    channelAffiliatedUnitsTempService.removeById(channel.getId());
                                    channelAffiliatedUnitsTemp.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                    channelAffiliatedUnitsTemp.setTotalApplicationNum(channel.getTotalApplicationNum());
                                    channelAffiliatedUnitsTemp.setMonthlySurplusNum(month <= 0 ? 0 : month);
                                    channelAffiliatedUnitsTemp.setTotalSurplusNum(total <= 0 ? 0 : total);
                                    channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                    channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                                    channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                    channelAffiliatedUnitsRelTemp.setArriliatedId(channelAffiliatedUnitsTemp.getId());
                                    channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                                    channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                                    channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                                    channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                                    return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channelAffiliatedUnitsTemp);

                                }
                            }
                        }
                    }
                } else if ((ChannelAffiiationEnum.STOP.getCode()).equals(oldInfo.get(i).getStatus())) {
                    return IResponse.fail("此挂靠公司已停用");
                } else {
                    List<ChannelAffiliatedUnitsRelTemp> list = channelAffiliatedUnitsRelTempService.list(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                            .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, oldInfo.get(i).getId())
                            .notIn(ChannelAffiliatedUnitsRelTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
                    int channelCount = 0;
                    if (list != null) {
                        for (int j = 0; j < list.size(); j++) {
                            if ((channel.getChannelId()).equals(list.get(j).getChannelId())) {
                                channelCount += 1;
                            }
                        }
                    } else {
                        channelCount = 0;
                    }
                    if (channelCount > 0) {
                        return IResponse.success(Boolean.FALSE).setMsg("该挂靠单位存在此合作商备案记录");
                    } else {
                        channel.setCreateTime(new Date());
                        channel.setCreateBy(SecurityUtils.getUsername());
                        if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
                            channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                            channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                        } else {
                            channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
                            channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
                            channel.setInLibraryOrNot(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.NOTINLIBARAY.getCode() : ChannelAffiiationEnum.INLIBARAY.getCode());
                            channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                            channel.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                            channel.setTotalSurplusNum(channel.getTotalApplicationNum());
                            channel.setTotalApplicationNum(channel.getTotalApplicationNum());
                        }
                        if (!channelAffiliatedUnitsTempService.save(channel)) {
                            return IResponse.fail("添加失败");
                        }
                        channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
                        channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                        channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                        channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                        channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                        return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel);
                    }
                }
            }
        } else {
            channel.setCreateBy(SecurityUtils.getUsername());
            channel.setCreateTime(new Date());
            if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
                channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
            } else {
                channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
                channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
                channel.setInLibraryOrNot(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.NOTINLIBARAY.getCode() : ChannelAffiiationEnum.INLIBARAY.getCode());
                channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                channel.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                channel.setTotalSurplusNum(channel.getTotalApplicationNum());
                channel.setTotalApplicationNum(channel.getTotalApplicationNum());
            }
            if (!channelAffiliatedUnitsTempService.save(channel)) {
                return IResponse.fail("添加失败");
            }
            channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
            channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
            channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
            channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
            channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
        }
        channel.setChannelIdReturn(channel.getChannelId().toString());
        if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode()) && saveOrCommit.equals(ChannelAffiiationEnum.COMMIT.getCode())) {
            List<Object> workfolwList = new ArrayList<>();
            workfolwList.add(channel);
            workfolwList.add(channelAffiliatedUnitsRelTemp);
            affiliatedThirdWorkflow(workfolwList);
        }
        return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel);
    }

    @PostMapping(value = "/addNetCarAffiliationInfo")
    @ApiOperation(value = "添加网约车挂靠公司信息")
    @SysLog("添加网约车挂靠公司信息")
    public IResponse addNetCarAffiliationInfo(@Valid @RequestBody AffiliationCondition channel) throws JsonProcessingException {
        String saveOrCommit = channel.getSaveOrCommit();
        channel.setApplyTime(new Date());
        ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = new ChannelAffiliatedUnitsRelTemp();
        //根据社会代码以及挂靠类型查询是否已存在挂靠公司
        List<ChannelAffiliatedUnitsTemp> oldInfo = channelAffiliatedUnitsTempService.list(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                .eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, channel.getSocUniCrtCode())
                .eq(ChannelAffiliatedUnitsTemp::getBusiType, channel.getBusiType())
                .notIn(ChannelAffiliatedUnitsTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
        if (oldInfo.size() > 0) {
            for (int i = 0; i < oldInfo.size(); i++) {
                List<ChannelAffiliatedUnitsRelTemp> list = channelAffiliatedUnitsRelTempService.list(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                        .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, oldInfo.get(i).getId())
                        .notIn(ChannelAffiliatedUnitsRelTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
                int channelCount = 0;
                //校验是否为同一合作商申请挂靠
                if (list != null) {
                    for (int j = 0; j < list.size(); j++) {
                        if ((channel.getChannelId()).equals(list.get(j).getChannelId())) {
                            channelCount += 1;
                        }
                    }
                } else {
                    channelCount = 0;
                }
                if (channelCount > 0) {
                    return IResponse.success(Boolean.FALSE).setMsg("该挂靠单位存在此合作商备案记录");
                } else {
                    if (oldInfo.get(i).getLimitValue() <= 0) {
                        return IResponse.fail("挂靠单位额度已用尽");
                    } else {
                    if ((ChannelAffiiationEnum.START.getCode()).equals(oldInfo.get(i).getStatus())) {
                        channel.setCreateBy(SecurityUtils.getUsername());
                        channel.setCreateTime(new Date());
                        if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
                            channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                            channelAffiliatedUnitsTempService.save(channel);
                            channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                            channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
                            channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                            channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                            channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                            channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                            return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel);
                        } else {
                            ChannelAffiliatedUnitsTemp channelAffiliatedUnitsTemp = channelAffiliatedUnitsTempService.getOne(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                                    .eq(ChannelAffiliatedUnitsTemp::getBusiType, channel.getBusiType())
                                    .eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, channel.getSocUniCrtCode())
                                    .eq(ChannelAffiliatedUnitsTemp::getStatus, ChannelAffiiationEnum.START.getCode()));
                            if (saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode())) {
                                channel.setStatus(ChannelAffiiationEnum.DEFAULT.getCode());
                                channelAffiliatedUnitsTempService.save(channel);
                                channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
                                channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.DEFAULT.getCode());
                                channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                                channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                                channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                                channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                                channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                                return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel.getId().toString());
                            } else if (saveOrCommit.equals(ChannelAffiiationEnum.COMMIT.getCode())) {
                                Long id = channelAffiliatedUnitsTemp.getId();
                                BeanUtils.copyProperties(channel, channelAffiliatedUnitsTemp);
//                                channelAffiliatedUnitsTempService.removeById(channel.getId());
                                channelAffiliatedUnitsTemp.setId(id);
                                channelAffiliatedUnitsTemp.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                channelAffiliatedUnitsTemp.setTotalApplicationNum(channel.getTotalApplicationNum());
                                channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                                channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsRelTemp.setArriliatedId(channelAffiliatedUnitsTemp.getId());
                                channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                                channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                                channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                                channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                                channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                                return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channelAffiliatedUnitsTemp.getId().toString());
                            }
                        }
                    } else if ((ChannelAffiiationEnum.STOP.getCode()).equals(oldInfo.get(i).getStatus())) {
                        return IResponse.success(Boolean.FALSE).setMsg("此挂靠公司已停用");
                    } else {
                        channel.setCreateBy(SecurityUtils.getUsername());
                        channel.setCreateTime(new Date());
                        if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
                            channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                            channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                        } else {
                            channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
                            channel.setInLibraryOrNot(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.NOTINLIBARAY.getCode() : ChannelAffiiationEnum.INLIBARAY.getCode());
                            channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
                        }
                        if (!channelAffiliatedUnitsTempService.save(channel)) {
                            return IResponse.fail("添加失败");
                        }
                        channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
                        channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
                        channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
                        channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
                        channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                        channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
                        channel.setChannelIdReturn(channel.getChannelId().toString());
                        return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel.getId().toString());
                    }
                }
                }
            }
        } else {
            channel.setCreateBy(SecurityUtils.getUsername());
            channel.setCreateTime(new Date());
            if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
                channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
                channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.REVIEW.getCode());
            } else {
                channel.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
                channel.setInLibraryOrNot(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.NOTINLIBARAY.getCode() : ChannelAffiiationEnum.INLIBARAY.getCode());
                channelAffiliatedUnitsRelTemp.setStatus(saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode()) ? ChannelAffiiationEnum.DEFAULT.getCode() : ChannelAffiiationEnum.START.getCode());
            }
            if (!channelAffiliatedUnitsTempService.save(channel)) {
                return IResponse.fail("添加失败");
            }
            channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
            channelAffiliatedUnitsRelTemp.setChannelId(channel.getChannelId());
            channelAffiliatedUnitsRelTemp.setCreateBy(SecurityUtils.getUsername());
            channelAffiliatedUnitsRelTemp.setCreateTime(new Date());
            channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
            channelAffiliatedUnitsRelTempService.save(channelAffiliatedUnitsRelTemp);
        }
        channel.setChannelIdReturn(channel.getChannelId().toString());
        if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode()) && saveOrCommit.equals(ChannelAffiiationEnum.COMMIT.getCode())) {
            List<Object> workfolwList = new ArrayList<>();
            workfolwList.add(channel);
            workfolwList.add(channelAffiliatedUnitsRelTemp);
            affiliatedNetCarWorkflow(workfolwList);
        }
        return IResponse.success(Boolean.TRUE).setMsg("录入成功").setData(channel.getId().toString());

    }

    @PostMapping(value = "/upDateAffiliationInfo")
    @ApiOperation(value = "修改第三方/网约车挂靠公司信息")
    @SysLog("修改第三方/网约车挂靠公司信息")
    public IResponse upDateAffiliationInfo(@Valid @RequestBody AffiliationCondition channel) throws JsonProcessingException {
        List<ChannelAffiliatedUnitsTemp> list = channelAffiliatedUnitsTempService.list(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda().eq(ChannelAffiliatedUnitsTemp::getBusiType, channel.getBusiType()).eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, channel.getSocUniCrtCode()).notIn(ChannelAffiliatedUnitsTemp::getStatus, Common.STATUS_REFUSE));
        boolean flag = false;
        for (int k = 0; k < list.size(); k++) {
            if (ChannelAffiiationEnum.STOP.getCode().equals(list.get(k).getStatus())) {
                flag = true;
                break;
            }
        }
        if (flag) {
            return IResponse.success(Boolean.FALSE).setMsg("此挂靠公司已停用");
        } else {
            int count = 0;
            if (list.size() > 0) {
                List<ChannelAffiliatedUnitsTemp> listOne = new ArrayList<>();
                for (int n = 0; n < list.size(); n++) {
                    if (!list.get(n).getId().equals(channel.getId())) {
                        listOne.add(list.get(n));
                    }
                }
                for (int i = 0; i < listOne.size(); i++) {
                    List<ChannelAffiliatedUnitsRelTemp> channelAffiliatedUnitsRelTempList = channelAffiliatedUnitsRelTempService.list(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                            .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, listOne.get(i).getId())
                            .notIn(ChannelAffiliatedUnitsRelTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
                    ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = new ChannelAffiliatedUnitsRelTemp();
                    if (channel.getChannelId() == null) {
                        channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId()));
                    } else {
                        channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId())
                                .eq(ChannelAffiliatedUnitsRelTemp::getChannelId, channel.getChannelId()));
                    }
                    for (int j = 0; j < channelAffiliatedUnitsRelTempList.size(); j++) {
                        if ((channelAffiliatedUnitsRelTemp.getChannelId()).equals(channelAffiliatedUnitsRelTempList.get(j).getChannelId())) {
                            count += 1;
                        }
                    }
                }
            }
            if (count > 0) {
                return IResponse.success(Boolean.FALSE).setMsg("该挂靠单位存在此合作商备案记录");
            } else {
                String saveOrCommit = channel.getSaveOrCommit();
                if (channel.getIsOrNotApply().equals(ChannelAffiiationEnum.ISAPPLY.getCode())) {
                    channel.setUpdateBy(SecurityUtils.getUsername());
                    channel.setUpdateTime(new Date());
                    channel.setIsOrNotApply(ChannelAffiiationEnum.ISAPPLY.getCode());
                    channelAffiliatedUnitsTempService.updateById(channel);
                    ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                            .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId())
                            .eq(ChannelAffiliatedUnitsRelTemp::getChannelId, channel.getChannelId()));
                    if ((channelAffiliatedUnitsRelTemp.getStatus()).equals(ChannelAffiiationEnum.DEFAULT.getCode())) {
                        if ((ChannelAffiiationEnum.SAVE.getCode()).equals(saveOrCommit)) {
                            channel.setStatus(ChannelAffiiationEnum.DEFAULT.getCode());
                        } else {
                            channel.setStatus(ChannelAffiiationEnum.REVIEW.getCode());
                        }
                    } else if ((channelAffiliatedUnitsRelTemp.getStatus()).equals(ChannelAffiiationEnum.STOP.getCode())) {
                        channel.setStatus(ChannelAffiiationEnum.REVIEW.getCode());
                    } else if ((channelAffiliatedUnitsRelTemp.getStatus()).equals(ChannelAffiiationEnum.BACK.getCode())) {
                        if ((ChannelAffiiationEnum.SAVE.getCode()).equals(saveOrCommit)) {
                            channel.setStatus(ChannelAffiiationEnum.BACK.getCode());
                        } else {
                            channel.setStatus(ChannelAffiiationEnum.REVIEW.getCode());
                        }
                    }
                    channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                    channelAffiliatedUnitsRelTemp.setStatus(channel.getStatus());
                    channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                    if (saveOrCommit.equals(ChannelAffiiationEnum.COMMIT.getCode()) && channel.getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())) {
                        List<Object> workfolwList = new ArrayList<>();
                        workfolwList.add(channel);
                        workfolwList.add(channelAffiliatedUnitsRelTemp);
                        affiliatedThirdWorkflow(workfolwList);
                    }else if(saveOrCommit.equals(ChannelAffiiationEnum.COMMIT.getCode()) && channel.getBusiType().equals(ChannelAffiiationEnum.NET_CAR.getCode())){
                        List<Object> workfolwList = new ArrayList<>();
                        workfolwList.add(channel);
                        workfolwList.add(channelAffiliatedUnitsRelTemp);
                        affiliatedNetCarWorkflow(workfolwList);
                    }
                } else {
                    if (saveOrCommit.equals(ChannelAffiiationEnum.SAVE.getCode())) {
                        channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                        channel.setTotalApplicationNum(channel.getTotalApplicationNum());
                        channel.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                        channel.setTotalSurplusNum(channel.getTotalApplicationNum());
                        channel.setStatus(ChannelAffiiationEnum.DEFAULT.getCode());
                        channel.setUpdateTime(new Date());
                        channel.setUpdateBy(SecurityUtils.getUsername());
                        channel.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                        channelAffiliatedUnitsTempService.updateById(channel);
                        List<ComAttachmentFile> comAttachmentFileListNew = this.comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                                .eq(ComAttachmentFile::getBusiNo,channel.getId().toString()));
                        if(comAttachmentFileListNew.size()>0){
                            ComAttachmentFileDto comAttachmentFileDto=new ComAttachmentFileDto();
                            comAttachmentFileDto.setComAttachmentFiles(comAttachmentFileListNew);
                                AfsTransEntity<ComAttachmentFileDto> transEntity = new AfsTransEntity<>();
                                transEntity.setTransCode(MqTransCode.CHANNEL_CHANNEL_POS_APPLY_SEND_FILE_TO_APPLY);
                                transEntity.setData(comAttachmentFileDto);
                                log.info("渠道挂靠影像信息……", transEntity);
                                channelFileToApplySenderl.pushFileToApply(transEntity);
                        }
                        if(channel.getChannelId()!=null){
                            ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                    .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId())
                                    .eq(ChannelAffiliatedUnitsRelTemp::getChannelId,channel.getChannelId()));
                            if(channel.getChannelTermValidity()!=null){
                                channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                            }
                            channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                        }
                    } else if (saveOrCommit.equals(ChannelAffiiationEnum.COMMIT.getCode())) {
                        if (list.size() > 0) {
                            for (int i = 0; i < list.size(); i++) {
                                if (ChannelAffiiationEnum.START.getCode().equals(list.get(i).getStatus())) {
                                    Long id = list.get(i).getId();
                                    List<ComAttachmentFile> comAttachmentFileListOld = this.comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                                            .eq(ComAttachmentFile::getBusiNo,id.toString()));
                                    List<ComAttachmentFile> comAttachmentFileListNew = this.comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                                            .eq(ComAttachmentFile::getBusiNo,channel.getId().toString()));
                                    if(comAttachmentFileListOld.size()>0){
                                        for(ComAttachmentFile comAttachmentFile:comAttachmentFileListOld){
                                            this.comAttachmentFileService.removeById(comAttachmentFile);
                                        }
                                    }
                                    List<ComAttachmentFile> comAttachmentFiles=new ArrayList();
                                    if(comAttachmentFileListNew.size()>0){
                                        for(ComAttachmentFile comAttachmentFile:comAttachmentFileListNew){
                                            comAttachmentFile.setBusiNo(id.toString());
                                            comAttachmentFile.setBelongNo(id.toString());
                                            comAttachmentFiles.add(comAttachmentFile);
                                            this.comAttachmentFileService.updateById(comAttachmentFile);
                                        }
                                        ComAttachmentFileDto comAttachmentFileDto=new ComAttachmentFileDto();
                                        comAttachmentFileDto.setComAttachmentFiles(comAttachmentFiles);
                                        AfsTransEntity<ComAttachmentFileDto> transEntity = new AfsTransEntity<>();
                                        transEntity.setTransCode(MqTransCode.CHANNEL_CHANNEL_POS_APPLY_SEND_FILE_TO_APPLY);
                                        transEntity.setData(comAttachmentFileDto);
                                        log.info("渠道挂靠影像信息……", transEntity);
                                        channelFileToApplySenderl.pushFileToApply(transEntity);
                                    }
                                    if (list.get(i).getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())) {
                                        channelAffiliatedUnitsTempService.removeById(channel.getId());
                                        int totalMonth = list.get(i).getMonthlyApplicationNum();
                                        int totalAll = list.get(i).getTotalApplicationNum();
                                        int totalSurplusMonth = list.get(i).getMonthlySurplusNum();
                                        int totalSurplusAll = list.get(i).getTotalSurplusNum();
                                        int total = channel.getTotalApplicationNum() - (totalAll - totalSurplusAll);
                                        int month = channel.getMonthlyApplicationNum() - (totalMonth - totalSurplusMonth);
                                        BeanUtils.copyProperties(channel, list.get(i));
                                        list.get(i).setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                        list.get(i).setTotalApplicationNum(channel.getTotalApplicationNum());
                                        list.get(i).setMonthlySurplusNum(month <= 0 ? 0 : month);
                                        list.get(i).setTotalSurplusNum(total <= 0 ? 0 : total);
                                        list.get(i).setStatus(ChannelAffiiationEnum.START.getCode());
                                        list.get(i).setUpdateTime(new Date());
                                        list.get(i).setId(id);
                                        list.get(i).setUpdateBy(SecurityUtils.getUsername());
                                        list.get(i).setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                        list.get(i).setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                        channelAffiliatedUnitsTempService.updateById(list.get(i));
                                    } else {
                                        channelAffiliatedUnitsTempService.removeById(channel.getId());
                                        BeanUtils.copyProperties(channel, list.get(i));
                                        list.get(i).setLimitValue(channel.getLimitValue());
                                        list.get(i).setWarningValue(channel.getWarningValue());
                                        list.get(i).setStatus(ChannelAffiiationEnum.START.getCode());
                                        list.get(i).setUpdateTime(new Date());
                                        list.get(i).setId(id);
                                        list.get(i).setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                        list.get(i).setUpdateBy(SecurityUtils.getUsername());
                                        list.get(i).setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                        channelAffiliatedUnitsTempService.updateById(list.get(i));
                                    }
                                    ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                            .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId()));
                                    channelAffiliatedUnitsRelTemp.setArriliatedId(id);
                                    channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                    channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                                    channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                                    if(channel.getChannelTermValidity()!=null){
                                        channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                                    }
                                    channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                                    return IResponse.success(Boolean.TRUE).setMsg("保存成功").setData(list.get(i));
                                } else {
                                    channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                    channel.setTotalApplicationNum(channel.getTotalApplicationNum());
                                    channel.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                                    channel.setTotalSurplusNum(channel.getTotalApplicationNum());
                                    channel.setStatus(ChannelAffiiationEnum.START.getCode());
                                    channel.setUpdateTime(new Date());
                                    channel.setUpdateBy(SecurityUtils.getUsername());
                                    channel.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                    channel.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                    channelAffiliatedUnitsTempService.updateById(channel);
                                    List<ComAttachmentFile> comAttachmentFileListNew = this.comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                                            .eq(ComAttachmentFile::getBusiNo,channel.getId().toString()));
                                    if(comAttachmentFileListNew.size()>0){
                                        ComAttachmentFileDto comAttachmentFileDto=new ComAttachmentFileDto();
                                        comAttachmentFileDto.setComAttachmentFiles(comAttachmentFileListNew);
                                        AfsTransEntity<ComAttachmentFileDto> transEntity = new AfsTransEntity<>();
                                        transEntity.setTransCode(MqTransCode.CHANNEL_CHANNEL_POS_APPLY_SEND_FILE_TO_APPLY);
                                        transEntity.setData(comAttachmentFileDto);
                                        log.info("渠道挂靠影像信息……", transEntity);
                                        channelFileToApplySenderl.pushFileToApply(transEntity);
                                    }
                                    ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                            .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId()));
                                    channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                    channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                                    channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                                    if(channel.getChannelTermValidity()!=null){
                                        channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                                    }
                                    channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                                }
                            }
                        } else {
                            if (ChannelAffiiationEnum.START.getCode().equals(channel.getStatus())) {
                                if (channel.getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())) {
                                    channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                    channel.setTotalApplicationNum(channel.getTotalApplicationNum());
                                    channel.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                                    channel.setTotalSurplusNum(channel.getTotalApplicationNum());
                                    channel.setStatus(ChannelAffiiationEnum.START.getCode());
                                    channel.setUpdateTime(new Date());
                                    channel.setUpdateBy(SecurityUtils.getUsername());
                                    channel.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                    channel.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                    channelAffiliatedUnitsTempService.updateById(channel);
                                } else {
                                    channel.setLimitValue(channel.getLimitValue());
                                    channel.setWarningValue(channel.getWarningValue());
                                    channel.setStatus(ChannelAffiiationEnum.START.getCode());
                                    channel.setUpdateTime(new Date());
                                    channel.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                    channel.setUpdateBy(SecurityUtils.getUsername());
                                    channel.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                    channelAffiliatedUnitsTempService.updateById(channel);
                                }
                                List<ComAttachmentFile> comAttachmentFileListNew = this.comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                                        .eq(ComAttachmentFile::getBusiNo,channel.getId().toString()));
                                if(comAttachmentFileListNew.size()>0){
                                    ComAttachmentFileDto comAttachmentFileDto=new ComAttachmentFileDto();
                                    comAttachmentFileDto.setComAttachmentFiles(comAttachmentFileListNew);
                                    AfsTransEntity<ComAttachmentFileDto> transEntity = new AfsTransEntity<>();
                                    transEntity.setTransCode(MqTransCode.CHANNEL_CHANNEL_POS_APPLY_SEND_FILE_TO_APPLY);
                                    transEntity.setData(comAttachmentFileDto);
                                    log.info("渠道挂靠影像信息……", transEntity);
                                    channelFileToApplySenderl.pushFileToApply(transEntity);
                                }
                                ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                        .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId()));
                                channelAffiliatedUnitsRelTemp.setArriliatedId(channel.getId());
                                channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                                channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                                if(channel.getChannelTermValidity()!=null){
                                    channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                                }
                                channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                                return IResponse.success(Boolean.TRUE).setMsg("保存成功").setData(channel);
                            } else {
                                channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                channel.setTotalApplicationNum(channel.getTotalApplicationNum());
                                channel.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                                channel.setTotalSurplusNum(channel.getTotalApplicationNum());
                                channel.setStatus(ChannelAffiiationEnum.START.getCode());
                                channel.setUpdateTime(new Date());
                                channel.setUpdateBy(SecurityUtils.getUsername());
                                channel.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                channel.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                channelAffiliatedUnitsTempService.updateById(channel);
                                List<ComAttachmentFile> comAttachmentFileListNew = this.comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda()
                                        .eq(ComAttachmentFile::getBusiNo,channel.getId().toString()));
                                if(comAttachmentFileListNew.size()>0){
                                    ComAttachmentFileDto comAttachmentFileDto=new ComAttachmentFileDto();
                                    comAttachmentFileDto.setComAttachmentFiles(comAttachmentFileListNew);
                                    AfsTransEntity<ComAttachmentFileDto> transEntity = new AfsTransEntity<>();
                                    transEntity.setTransCode(MqTransCode.CHANNEL_CHANNEL_POS_APPLY_SEND_FILE_TO_APPLY);
                                    transEntity.setData(comAttachmentFileDto);
                                    log.info("渠道挂靠影像信息……", transEntity);
                                    channelFileToApplySenderl.pushFileToApply(transEntity);
                                }
                                ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                                        .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId()));
                                channelAffiliatedUnitsRelTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                                channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                                if(channel.getChannelTermValidity()!=null){
                                    channelAffiliatedUnitsRelTemp.setChannelTermValidity(channel.getChannelTermValidity());
                                }
                                channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                            }
                        }

                    }
                }
            }
            if (channel.getChannelId() != null) {
                channel.setChannelIdReturn(channel.getChannelId().toString());
            }
            return IResponse.success(Boolean.TRUE).setMsg("修改成功").setData(channel);
        }
    }

    @PostMapping(value = "/upDateStopAffiliationInfo")
    @ApiOperation(value = "修改停用挂靠公司信息")
    @SysLog("修改停用挂靠公司信息")
    public IResponse upDateStopAffiliationInfo(@Valid @RequestBody AffiliationCondition channel) {
        ChannelAffiliatedUnitsTemp list = channelAffiliatedUnitsTempService.getOne(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda().eq(ChannelAffiliatedUnitsTemp::getId, channel.getId()));
        if (list.getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())) {
            int totalMonth = list.getMonthlyApplicationNum();
            int totalAll = list.getTotalApplicationNum();
            int totalSurplusMonth = list.getMonthlySurplusNum();
            int totalSurplusAll = list.getTotalSurplusNum();
            int total = channel.getTotalApplicationNum() - (totalAll - totalSurplusAll);
            int month = channel.getMonthlyApplicationNum() - (totalMonth - totalSurplusMonth);
            channel.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
            channel.setTotalApplicationNum(channel.getTotalApplicationNum());
            channel.setMonthlySurplusNum(month <= 0 ? 0 : month);
            channel.setTotalSurplusNum(total <= 0 ? 0 : total);
        } else {
            channel.setWarningValue(channel.getWarningValue());
            channel.setLimitValue(channel.getLimitValue());
        }
        channel.setStatus(ChannelAffiiationEnum.START.getCode());
        channel.setUpdateBy(SecurityUtils.getUsername());
        channel.setUpdateTime(new Date());
        channelAffiliatedUnitsTempService.updateById(channel);
        return IResponse.success(Boolean.TRUE).setMsg("修改成功").setData(channel);
    }

    /**
     * 渠道端停用挂靠
     */
    @PostMapping(value = "/upDateStatusByIds/{ids}")
    @ApiOperation(value = "根据id停用挂靠公司及关联合作商关系")
    public IResponse upDateStatusByIds(@PathVariable String ids) {
        //截取字符串
        String[] idArray = ids.split(",");
        for (int j = 0; j < idArray.length; j++) {
            Long id = Long.valueOf(idArray[j].toString());
            ChannelAffiliatedUnitsTemp channel = channelAffiliatedUnitsTempService.getById(id);
            if ((ChannelAffiiationEnum.START.getCode()).equals(channel.getStatus())) {
                channel.setStatus(ChannelAffiiationEnum.STOP.getCode());
                channel.setUpdateTime(new Date(System.currentTimeMillis()));
                channel.setUpdateBy(SecurityUtils.getUsername());
            }
            channelAffiliatedUnitsTempService.updateById(channel);
            List<ChannelAffiliatedUnitsRelTemp> channelAffiliatedUnitsRelTempList = channelAffiliatedUnitsRelTempService.list(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                    .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId()).notIn(ChannelAffiliatedUnitsRelTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
            for (int i = 0; i < channelAffiliatedUnitsRelTempList.size(); i++) {
                channelAffiliatedUnitsRelTempList.get(i).setStatus(ChannelAffiiationEnum.STOP.getCode());
                channelAffiliatedUnitsRelTempList.get(i).setUpdateBy(SecurityUtils.getUsername());
                channelAffiliatedUnitsRelTempList.get(i).setUpdateTime(new Date());
                channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTempList.get(i));
            }
        }
        return new IResponse<Boolean>().setMsg("状态更新成功");
    }

    /**
     * 进件端停用挂靠
     */
    @PostMapping(value = "/upDateStatusByIdForApply")
    @ApiOperation(value = "根据id停用挂靠公司")
    public IResponse upDateStatusByIdForApply(@RequestBody String ids) {
        //截取字符串
        String[] idArray = ids.split(",");
        for (int j = 0; j < idArray.length; j++) {
            Long id = Long.valueOf(idArray[j]);
            ChannelAffiliatedUnitsRelTemp channel = channelAffiliatedUnitsRelTempService.getById(id);
            channel.setStatus(ChannelAffiiationEnum.STOP.getCode());
            channel.setUpdateTime(new Date(System.currentTimeMillis()));
            channel.setUpdateBy(SecurityUtils.getUsername());
            channelAffiliatedUnitsRelTempService.updateById(channel);
        }
        return new IResponse<Boolean>().setMsg("状态更新成功");
    }

    @PostMapping(value = "/getChannelFullName")
    @ApiOperation(value = "根据id获取合作商信息")
    public IResponse getChannelFullName(@RequestBody AffiliationCondition condition) {
        List statusList = new ArrayList();
        statusList.add(ChannelAffiiationEnum.REVIEW.getCode());
        statusList.add(ChannelAffiiationEnum.DEFAULT.getCode());
        statusList.add(ChannelAffiiationEnum.BACK.getCode());
        statusList.add(ChannelAffiiationEnum.START.getCode());
        statusList.add(ChannelAffiiationEnum.STOP.getCode());
        condition.setQueryStatus(statusList);
        //  businessType：01是新车  02 二手车  03是非直营新车二手车都有
        // ownBusinessType ：01是新车  02 二手车  03  是直营新车二手车都有

        // 第一步我要知道登录者的角色属性  是属于直营还是非直营
        List<String> role =SecurityUtils.getRoles();
        BaseInfoTypeDTO dto =new BaseInfoTypeDTO();
        List belongList=new ArrayList();
        if(role.contains(ChannelRoleEnum.ROLE_ADMIN.name())){
            // 说明进来的是管理员
            dto.setBusinessType(RoleTypeDic.ALL_CAR);
            dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);
            belongList.add(AfsEnumUtil.key(ChannelBelongEnum.DIRECT));
            belongList.add(AfsEnumUtil.key(ChannelBelongEnum.SP));
            belongList.add(AfsEnumUtil.key(ChannelBelongEnum.ALL_TO_ALL));
            dto.setChannelBelong(belongList);
        }else {
            if(role.contains(ChannelRoleEnum.ROLE_OTHER_MANAGE_THIRD_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_SEE_THIRD_AFFILIATED.name())
                    || role.contains(ChannelRoleEnum.ROLE_OTHER_MANAGE_NET_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OTHER_SEE_NET_AFFILIATED.name())){
                // 说明我有非直营权限
                dto.setBusinessType(RoleTypeDic.ALL_CAR);
                belongList.add(AfsEnumUtil.key(ChannelBelongEnum.SP));
                belongList.add(AfsEnumUtil.key(ChannelBelongEnum.ALL_TO_ALL));
                dto.setChannelBelong(belongList);
            }
            if(role.contains(ChannelRoleEnum.ROLE_OWN_MANAGE_THIRD_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OWN_SEE_THIRD_AFFILIATED.name())
                    ||role.contains(ChannelRoleEnum.ROLE_OWN_MANAGE_NET_AFFILIATED.name())||role.contains(ChannelRoleEnum.ROLE_OWN_SEE_NET_AFFILIATED.name())){
                // 说明我有直营权限
                dto.setOwnBusinessType(RoleTypeDic.ALL_CAR);
                belongList.add(AfsEnumUtil.key(ChannelBelongEnum.DIRECT));
                dto.setChannelBelong(belongList);
            }
        }
        if((dto.getBusinessType()==null||dto.getBusinessType()=="")&&(dto.getOwnBusinessType()==null||dto.getOwnBusinessType()=="")){
            return IResponse.fail("暂无权限查询！");
        }
        Map<String,String> headers = new HashMap<String,String>();
        headers.put("clientId",channelConfig.getCaseClientId());
        headers.put("clientSecret",channelConfig.getCaseClientSecret());
        IResponse result=channelUseCaseService.getBaseInfoByRole(dto,headers);
        List<ChannelBaseInfoTempVo> list=null;
        if("0000".equals(result.getCode())) {
            List<Long> ids = new ArrayList<>();
            List<ChannelBaseInfoTemp> base = JSON.parseArray(JSON.toJSONString(result.getData()), ChannelBaseInfoTemp.class);
            if (base.size() > 0) {
                for (int i = 0; i < base.size(); i++) {
                    ids.add(base.get(i).getId());
                }
                 list= channelBaseInfoTempService.getChannelBaseInfo(condition,ids);
            }
        }
        return IResponse.success(list);
    }

    @PostMapping(value = "/getAffiliationInfoById")
    @ApiOperation(value = "根据id获取挂靠公司信息")
    public IResponse getAffiliationInfoById(@RequestBody AffiliationCondition channel) {
        Long thisId = Long.valueOf(channel.getId());
        ChannelAffiliatedUnitsTemp list = channelAffiliatedUnitsTempService.getById(thisId);
        if(channel.getChannelId()!=null && !("").equals(channel.getChannelId())){
            ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp=channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda().eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId,thisId).eq(ChannelAffiliatedUnitsRelTemp::getChannelId,channel.getChannelId()));
            if(channelAffiliatedUnitsRelTemp!=null){
                list.setChannelTermValidity(channelAffiliatedUnitsRelTemp.getChannelTermValidity());
            }
        }
        return IResponse.success(list);
    }

    @PostMapping(value = "/getAffiliationInfo")
    @ApiOperation(value = "获取启用挂靠公司信息")
    public IResponse getAffiliationInfo(@RequestBody AffiliationCondition channel) {
        Long thisId = Long.valueOf(channel.getId());
        ChannelAffiliatedUnitsTemp list = channelAffiliatedUnitsTempService.getById(thisId);
        ChannelAffiliatedUnitsTemp channelAffiliatedUnitsTemp = channelAffiliatedUnitsTempService.getOne(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                .eq(ChannelAffiliatedUnitsTemp::getStatus, ChannelAffiiationEnum.START.getCode())
                .eq(ChannelAffiliatedUnitsTemp::getBusiType, list.getBusiType())
                .eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, list.getSocUniCrtCode()));
        if (channelAffiliatedUnitsTemp != null) {
            list.setInLibraryOrNot(channelAffiliatedUnitsTemp.getInLibraryOrNot());
            if (ChannelAffiiationEnum.NET_CAR.getCode().equals(list.getBusiType())) {
                list.setLimitValue(channelAffiliatedUnitsTemp.getLimitValue());
                list.setWarningValue(channelAffiliatedUnitsTemp.getWarningValue());
            } else {
                list.setTotalApplicationNum(channelAffiliatedUnitsTemp.getTotalApplicationNum());
                list.setMonthlyApplicationNum(channelAffiliatedUnitsTemp.getMonthlyApplicationNum());
            }
        } else {
            list.setInLibraryOrNot(ChannelAffiiationEnum.NOTINLIBARAY.getCode());
        }
        return IResponse.success(list);
    }

    @PostMapping(value = "/examineThird")
    @ApiOperation(value = "审批第三方/网约车挂靠及合作商关系")
    public IResponse examineThird(@Valid @RequestBody AffiliationCondition channel) {
        List<String> roles =SecurityUtils.getRoles();
        WorkTaskAssign assgin = workTaskAssignService.getOne(Wrappers.<WorkTaskAssign>query().lambda().eq(WorkTaskAssign::getBizDataId, channel.getBizDataId())
                .in(WorkTaskAssign::getUserName, roles));
        WorkTaskCreated created = workTaskCreatedService.getOne(Wrappers.<WorkTaskCreated>query().lambda().eq(WorkTaskCreated::getBizDataId, channel.getBizDataId()));
        WorkTaskDetail detail = workTaskDetailService.getOne(Wrappers.<WorkTaskDetail>query().lambda().eq(WorkTaskDetail::getBizDataId, channel.getBizDataId()));

        //提交完成之后删除其他人员代办
        List<WorkTaskAssign> assignList = workTaskAssignService.list(Wrappers.<WorkTaskAssign>query().lambda().eq(WorkTaskAssign::getBizDataId, channel.getBizDataId()));
        TaskSubmitRequest taskSubmitRequest = new TaskSubmitRequest();
        taskSubmitRequest.setGwtUserId(assgin.getAssignId());
        taskSubmitRequest.setAfsFlowKey(created.getAfsFlowKey());
        taskSubmitRequest.setBizDataId(created.getBizDataId());
        QueryFLowCmdRequest request = new QueryFLowCmdRequest();
        request.setFlowInstanceId(created.getFlowInstanceId());
        WorkFlowResponse<List<FlowCmdBean>> cmdResponse = workflowService.queryFlowCmd(request);
        List<FlowCmdBean> cmdList = cmdResponse.getAfsData();
        FlowCmdBean cmd = workTaskCreatedService.affiliateApprove(channel.getStatus(), cmdList);
        String affiliationId = "";
        if(cmd!=null) {
            log.info("==================================提交工作流开始==================================》");
            taskSubmitRequest.setCmdId(cmd.getId());
            //获取配置节点信息
            JSONObject cancelJson = new JSONObject();
            cancelJson.put(AfsEnumUtil.key(ChannelAfsFlowEnum.BUSI_TYPE), detail.getBusinessType());
            workflowService.taskSubmit(taskSubmitRequest, cancelJson);
            channel.setTaskName(assgin.getTaskName());
            workTaskApproveRecordService.approveAffiliationSave(channel, cmd);
            log.info("==================================业务逻辑处理开始==================================》");
            String taskMemo = created.getTaskMemo();
            if (!StrUtil.isBlank(taskMemo)) {
                JSONObject jsonMemo = (JSONObject) JSONObject.parse(taskMemo);
                String memo = (String) jsonMemo.get(AfsEnumUtil.key(ChannelAfsFlowEnum.NODE_LABEL));
                ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = channelAffiliatedUnitsRelTempService.getOne(Wrappers.<ChannelAffiliatedUnitsRelTemp>query().lambda()
                        .eq(ChannelAffiliatedUnitsRelTemp::getArriliatedId, channel.getId())
                        .eq(ChannelAffiliatedUnitsRelTemp::getChannelId, channel.getChannelId()));
                List<ChannelAffiliatedUnitsTemp> oldInfo = channelAffiliatedUnitsTempService.list(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                        .eq(ChannelAffiliatedUnitsTemp::getBusiType, channel.getBusiType())
                        .eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, channel.getSocUniCrtCode())
                        .notIn(ChannelAffiliatedUnitsTemp::getStatus, ChannelAffiiationEnum.REFUSE.getCode()));
                int start = 0;
                int stop = 0;
                if (oldInfo.size() > 0) {
                    for (int i = 0; i < oldInfo.size(); i++) {
                        if (ChannelAffiiationEnum.START.getCode().equals(oldInfo.get(i).getStatus())) {
                            start += 1;
                        } else if (ChannelAffiiationEnum.STOP.getCode().equals(oldInfo.get(i).getStatus())) {
                            stop += 1;
                        }
                    }
                    if (stop > 0) {
                        return IResponse.fail("此挂靠公司已停用!");
                    }
                    if (start > 0) {
                        ChannelAffiliatedUnitsTemp channelAffiliatedUnitsTemp = channelAffiliatedUnitsTempService.getOne(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                                .eq(ChannelAffiliatedUnitsTemp::getBusiType, channel.getBusiType())
                                .eq(ChannelAffiliatedUnitsTemp::getSocUniCrtCode, channel.getSocUniCrtCode())
                                .eq(ChannelAffiliatedUnitsTemp::getStatus, ChannelAffiiationEnum.START.getCode()));
                        Long id = channelAffiliatedUnitsTemp.getId();
                        if (ChannelAffiiationEnum.START.getCode().equals(channel.getStatus())) {
                            affiliationId = id.toString();
                            if(!affiliationId.equals(channel.getId().toString())){
                                List<ComAttachmentFile> comAttachmentFileListOld=comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo,affiliationId));
                                List<ComAttachmentFile> comAttachmentFileListNew=comAttachmentFileService.list(Wrappers.<ComAttachmentFile>query().lambda().eq(ComAttachmentFile::getBusiNo,channel.getId().toString()));
                                if(comAttachmentFileListOld.size()>0){
                                    for(ComAttachmentFile comAttachmentFile:comAttachmentFileListOld){
                                        this.comAttachmentFileService.removeById(comAttachmentFile);
                                    }
                                }
                                if(comAttachmentFileListNew.size()>0){
                                    for(ComAttachmentFile comAttachmentFile:comAttachmentFileListNew){
                                        comAttachmentFile.setBusiNo(affiliationId);
                                        comAttachmentFile.setBelongNo(affiliationId);
                                        this.comAttachmentFileService.updateById(comAttachmentFile);
                                    }
                                }
                                channelAffiliatedUnitsTempService.removeById(channel.getId());
                            }
                            if (channelAffiliatedUnitsTemp.getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())) {
                                int totalMonth = channelAffiliatedUnitsTemp.getMonthlyApplicationNum();
                                int totalAll = channelAffiliatedUnitsTemp.getTotalApplicationNum();
                                int totalSurplusMonth = channelAffiliatedUnitsTemp.getMonthlySurplusNum();
                                int totalSurplusAll = channelAffiliatedUnitsTemp.getTotalSurplusNum();
                                int total = channel.getTotalApplicationNum() - (totalAll - totalSurplusAll);
                                int month = channel.getMonthlyApplicationNum() - (totalMonth - totalSurplusMonth);
                                BeanUtils.copyProperties(channel, channelAffiliatedUnitsTemp);
                                channelAffiliatedUnitsTemp.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                channelAffiliatedUnitsTemp.setTotalApplicationNum(channel.getTotalApplicationNum());
                                channelAffiliatedUnitsTemp.setMonthlySurplusNum(month <= 0 ? 0 : month);
                                channelAffiliatedUnitsTemp.setTotalSurplusNum(total <= 0 ? 0 : total);
                                channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                                channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                                channelAffiliatedUnitsTemp.setId(id);
                                channelAffiliatedUnitsTemp.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                channelAffiliatedUnitsTemp.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                            } else {
                                BeanUtils.copyProperties(channel, channelAffiliatedUnitsTemp);
                                channelAffiliatedUnitsTemp.setLimitValue(channel.getLimitValue());
                                channelAffiliatedUnitsTemp.setWarningValue(channel.getWarningValue());
                                channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                                channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                                channelAffiliatedUnitsTemp.setId(id);
                                channelAffiliatedUnitsTemp.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                channelAffiliatedUnitsTemp.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                            }
                            channelAffiliatedUnitsRelTemp.setArriliatedId(id);
                            channelAffiliatedUnitsRelTemp.setStatus(channel.getStatus());
                            channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                        } else {
                            channelAffiliatedUnitsTemp.setStatus(channel.getStatus());
                            channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                            channelAffiliatedUnitsRelTemp.setStatus(channel.getStatus());
                            channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                            if (ChannelAffiiationEnum.BACK.getCode().equals(cmd.getCmdDis())) {
                                if (ChannelOnlineConstants.FIRST_NODE.equals(memo)) {
                                    workTaskDetailService.removeById(detail);
                                    workTaskCreatedService.removeById(created);
                                }
                            }
                        }
                    } else {
                        ChannelAffiliatedUnitsTemp channelAffiliatedUnitsTemp = channelAffiliatedUnitsTempService.getOne(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                                .eq(ChannelAffiliatedUnitsTemp::getId, channel.getId()));
                        if (ChannelAffiiationEnum.START.getCode().equals(channel.getStatus())) {
                            affiliationId = channel.getId().toString();
                            if (channelAffiliatedUnitsTemp.getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())) {
                                channelAffiliatedUnitsTemp.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                                channelAffiliatedUnitsTemp.setTotalApplicationNum(channel.getTotalApplicationNum());
                                channelAffiliatedUnitsTemp.setMonthlySurplusNum(channel.getMonthlyApplicationNum());
                                channelAffiliatedUnitsTemp.setTotalSurplusNum(channel.getTotalApplicationNum());
                                channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                                channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                                channelAffiliatedUnitsTemp.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                channelAffiliatedUnitsTemp.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                            } else {
                                channelAffiliatedUnitsTemp.setLimitValue(channel.getLimitValue());
                                channelAffiliatedUnitsTemp.setWarningValue(channel.getWarningValue());
                                channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                                channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                                channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                                channelAffiliatedUnitsTemp.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                                channelAffiliatedUnitsTemp.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                                channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                            }
                            channelAffiliatedUnitsRelTemp.setStatus(channel.getStatus());
                            channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                        } else {
                            channelAffiliatedUnitsTemp.setStatus(channel.getStatus());
                            channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                            channelAffiliatedUnitsRelTemp.setStatus(channel.getStatus());
                            channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                            if (ChannelAffiiationEnum.BACK.getCode().equals(cmd.getCmdDis())) {
                                if (ChannelOnlineConstants.FIRST_NODE.equals(memo)) {
                                    workTaskDetailService.removeById(detail);
                                    workTaskCreatedService.removeById(created);
                                }
                            }
                        }
                    }
                } else {
                    ChannelAffiliatedUnitsTemp channelAffiliatedUnitsTemp = channelAffiliatedUnitsTempService.getOne(Wrappers.<ChannelAffiliatedUnitsTemp>query().lambda()
                            .eq(ChannelAffiliatedUnitsTemp::getId, channel.getId()));
                    if (ChannelAffiiationEnum.START.getCode().equals(channel.getStatus())) {
                        affiliationId = channel.getId().toString();
                        if (channelAffiliatedUnitsTemp.getBusiType().equals(ChannelAffiiationEnum.THIRD.getCode())) {
                            int totalMonth = channelAffiliatedUnitsTemp.getMonthlyApplicationNum();
                            int totalAll = channelAffiliatedUnitsTemp.getTotalApplicationNum();
                            int totalSurplusMonth = channelAffiliatedUnitsTemp.getMonthlySurplusNum();
                            int totalSurplusAll = channelAffiliatedUnitsTemp.getTotalSurplusNum();
                            int total = channel.getTotalApplicationNum() - (totalAll - totalSurplusAll);
                            int month = channel.getMonthlyApplicationNum() - (totalMonth - totalSurplusMonth);
                            channelAffiliatedUnitsTemp.setMonthlyApplicationNum(channel.getMonthlyApplicationNum());
                            channelAffiliatedUnitsTemp.setTotalApplicationNum(channel.getTotalApplicationNum());
                            channelAffiliatedUnitsTemp.setMonthlySurplusNum(month <= 0 ? 0 : month);
                            channelAffiliatedUnitsTemp.setTotalSurplusNum(total <= 0 ? 0 : total);
                            channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                            channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsTemp.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                            channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsTemp.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                            channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                        } else {
                            channelAffiliatedUnitsTemp.setLimitValue(channel.getLimitValue());
                            channelAffiliatedUnitsTemp.setWarningValue(channel.getWarningValue());
                            channelAffiliatedUnitsTemp.setStatus(ChannelAffiiationEnum.START.getCode());
                            channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                            channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                            channelAffiliatedUnitsTemp.setInLibraryOrNot(ChannelAffiiationEnum.INLIBARAY.getCode());
                            channelAffiliatedUnitsTemp.setIsOrNotApply(ChannelAffiiationEnum.NOTAPPLY.getCode());
                            channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                        }
                        channelAffiliatedUnitsRelTemp.setStatus(channel.getStatus());
                        channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                        channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                        channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                    } else {
                        channelAffiliatedUnitsTemp.setStatus(channel.getStatus());
                        channelAffiliatedUnitsTemp.setUpdateTime(new Date());
                        channelAffiliatedUnitsTemp.setUpdateBy(SecurityUtils.getUsername());
                        channelAffiliatedUnitsTempService.updateById(channelAffiliatedUnitsTemp);
                        channelAffiliatedUnitsRelTemp.setStatus(channel.getStatus());
                        channelAffiliatedUnitsRelTemp.setUpdateTime(new Date());
                        channelAffiliatedUnitsRelTemp.setUpdateBy(SecurityUtils.getUsername());
                        channelAffiliatedUnitsRelTempService.updateById(channelAffiliatedUnitsRelTemp);
                        if (ChannelAffiiationEnum.BACK.getCode().equals(cmd.getCmdDis())) {
                            if (ChannelOnlineConstants.FIRST_NODE.equals(memo)) {
                                workTaskDetailService.removeById(detail);
                                workTaskCreatedService.removeById(created);
                            }
                        }
                    }
                }
            }
            //删除剩余节点数据信息
            for (int i = 0; i < assignList.size(); i++) {
                workTaskAssignService.removeById(assignList.get(i).getId().toString());
            }
            return IResponse.success(Boolean.TRUE).setMsg("保存成功").setData(affiliationId);
        }else{
            return IResponse.fail("提交失败,提交指令异常，请确认！");
        }
    }

    @RequestMapping(value = "/getAffiliation", method = RequestMethod.POST)
    @ApiOperation(value = "根据条件获取挂靠公司列表")
    public IResponse getAffiliation(@ModelAttribute AffiliationCondition channel) {
        Page page = new Page(channel.getPageNumber(), channel.getPageSize());
        IPage<List<ChannelAffiliatedUnitsTemp>> list = channelAffiliatedUnitsTempService.getChannelAffiliatedUnitsTemp(page, channel);
        return IResponse.success(list);
    }

    @PostMapping(value = "/getMainAreaById")
    @ApiOperation(value = "根据渠道id获取授权区域信息")
    public IResponse<List<ChannelAuthorizeRegionTemp>> getMainAreaById(@RequestBody ChannelAuthorizeRegionTemp channelAuthorizeRegionTemp) {
        List<ChannelAuthorizeRegionTemp> list = channelAuthorizeRegionService.list(Wrappers.<ChannelAuthorizeRegionTemp>query().lambda().eq(ChannelAuthorizeRegionTemp::getChannelId, channelAuthorizeRegionTemp.getId().toString())
                .eq(ChannelAuthorizeRegionTemp::getBusinessType,BUSINESS_TYPE_NEW_CAR));
        return new IResponse<List<ChannelAuthorizeRegionTemp>>().setData(list);
    }

    /**
     * 三方挂靠公司提交工作流公用方法
     */
    public void affiliatedThirdWorkflow(List<Object> temp) throws JsonProcessingException {
        ChannelAffiliatedUnitsTemp affiliatedUnitsTemp = (ChannelAffiliatedUnitsTemp) temp.get(0);
        ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = (ChannelAffiliatedUnitsRelTemp) temp.get(1);
        StartFlowRequest startFlow = new StartFlowRequest();
        //枚举第三方挂靠
        startFlow.setAfsFlowKey(AfsEnumUtil.key(ChannelAfsFlowEnum.AFFILIATED_THIRD));
        startFlow.setBizDataId(uidGenerator.getUID() + "");
        //枚举第三方挂靠
        startFlow.setFlowName((StringUtils.isEmpty(affiliatedUnitsTemp.getAffiliatedName())?"":(affiliatedUnitsTemp.getAffiliatedName())+"-")+AfsEnumUtil.desc(ChannelAfsFlowEnum.AFFILIATED_THIRD));
        JSONObject jsonObject=new JSONObject();
        ChannelBaseInfoTemp channelBaseInfoTemp=channelBaseInfoTempService.getById(channelAffiliatedUnitsRelTemp.getChannelId());
        jsonObject.put(AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_BELONG),channelBaseInfoTemp.getChannelBelong());
        jsonObject.put(AfsEnumUtil.key(ChannelAfsFlowEnum.BUSI_TYPE),ChannelAffiiationEnum.THIRD.getCode());
        workflowService.startWorkFlow(startFlow,jsonObject);
        //保存业务相关数据、用户前端逻辑判断
        WorkTaskDetail detail = new WorkTaskDetail();
        detail.setBizDataId(startFlow.getBizDataId());
        detail.setName(affiliatedUnitsTemp.getAffiliatedName());
        detail.setFlowName(AfsEnumUtil.desc(ChannelAfsFlowEnum.AFFILIATED_THIRD));
        //挂靠公司,渠道id绑定
        detail.setBusinessKey(affiliatedUnitsTemp.getId());
        detail.setBusinessType(ChannelAffiiationEnum.THIRD.getCode());
        detail.setChannelId(channelAffiliatedUnitsRelTemp.getChannelId());
        workTaskDetailService.save(detail);
    }

    /**
     * 网约车挂靠公司提交工作流公用方法
     */
    public void affiliatedNetCarWorkflow(List<Object> temp) throws JsonProcessingException {
        ChannelAffiliatedUnitsTemp affiliatedUnitsTemp = (ChannelAffiliatedUnitsTemp) temp.get(0);
        ChannelAffiliatedUnitsRelTemp channelAffiliatedUnitsRelTemp = (ChannelAffiliatedUnitsRelTemp) temp.get(1);
        StartFlowRequest startFlow = new StartFlowRequest();
        //枚举网约车挂靠
        startFlow.setAfsFlowKey(AfsEnumUtil.key(ChannelAfsFlowEnum.AFFILIATED_THIRD));
        startFlow.setBizDataId(uidGenerator.getUID() + "");
        //枚举网约车挂靠
        startFlow.setFlowName((StringUtils.isEmpty(affiliatedUnitsTemp.getAffiliatedName())?"":(affiliatedUnitsTemp.getAffiliatedName())+"-")+AfsEnumUtil.desc(ChannelAfsFlowEnum.AFFILIATED_THIRD));
        JSONObject jsonObject=new JSONObject();
        ChannelBaseInfoTemp channelBaseInfoTemp=channelBaseInfoTempService.getById(channelAffiliatedUnitsRelTemp.getChannelId());
        jsonObject.put(AfsEnumUtil.key(ChannelAfsFlowEnum.CHANNEL_BELONG),channelBaseInfoTemp.getChannelBelong());
        jsonObject.put(AfsEnumUtil.key(ChannelAfsFlowEnum.BUSI_TYPE),ChannelAffiiationEnum.NET_CAR.getCode());
        workflowService.startWorkFlow(startFlow, jsonObject);
        //保存业务相关数据、用户前端逻辑判断
        WorkTaskDetail detail = new WorkTaskDetail();
        detail.setBizDataId(startFlow.getBizDataId());
        detail.setName(affiliatedUnitsTemp.getAffiliatedName());
        detail.setFlowName(AfsEnumUtil.desc(ChannelAfsFlowEnum.AFFILIATED_THIRD));
        //挂靠公司,渠道id绑定
        detail.setBusinessKey(affiliatedUnitsTemp.getId());
        detail.setChannelId(channelAffiliatedUnitsRelTemp.getChannelId());
        detail.setBusinessType(ChannelAffiiationEnum.NET_CAR.getCode());
        workTaskDetailService.save(detail);
    }
}
